<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-05-14 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-20 12:40:32
 */

namespace app\weixin_manage\controller;
use app\common\controller\Backend;

class User extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '用户', '公众号管理', 'user');

		$this->wechatUserDao = model('weixin_manage/WeixinUser');
	}

	function index() {
		if (!$this->wid) {
			$this->adminError('微信wid有误');
		}
		list($map, $parameter) = $this->getMap();

		$count = $this->wechatUserDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list, $ids) = $this->wechatUserDao->search($map, $Page->getLimit());
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		return $this->fetch();
	}

	private function getMap() {
		$map = $parameter = array();

		$wid = input('param.wid', '', 'intval');
		if ($wid) {
			$map['wid'] = $wid;
			$parameter['wid'] = $wid;
		}
		$this->assign('wid', $wid);

		$id = input('param.id', '', 'intval');
		if ($id) {
			$map[] = ['id', '=', $id];
			$parameter['id'] = $id;
		}
		$this->assign('id', $id);

		$nickname = input('param.nickname', '', 'pwEscape');
		if ($nickname) {
			$map['nickname'] = array('like', '%'.$nickname.'%');
			$parameter['nickname'] = $nickname;
		}
		$this->assign('nickname', $nickname);

		$openid = input('param.openid', '', 'pwEscape');
		if ($openid) {
			$map['openid'] = $openid;
			$parameter['openid'] = $openid;
		}
		$this->assign('openid', $openid);

		$unionid = input('param.unionid', '', 'pwEscape');
		if ($unionid) {
			$map['unionid'] = $unionid;
			$parameter['unionid'] = $unionid;
		}
		$this->assign('unionid', $unionid);

		return array($map, $parameter);
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');
		if (!$id) {
			$this->error('非法ID');
		}

		$result = $this->wechatUserDao->delInfo($id);
		if ($result !== false) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败'.showDbError($this->wechatUserDao));
		}
	}

}